package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.data.ContactMapper;
import io.xxx.xbutler.data.RobotContactMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Contact;
import io.xxx.xbutler.domain.GenderType;
import io.xxx.xbutler.domain.RobotContact;
import jakarta.annotation.Resource;

import java.time.LocalDateTime;

public abstract class RobotContactHandler {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private ContactMapper contactMapper;

    @Resource
    private RobotContactMapper robotContactMapper;

    public void saveRobotContact(Callback callback, JSONObject data, LocalDateTime now) {
        String robotId = callback.getRobotId();
        String robotSerialNo = callback.getRobotSerialNo();
        Contact contact = buildContact(data, now);
        boolean contactExists = isExists(contact.getId());
        if (contactExists) {
            contactMapper.updateById(contact);
        } else {
            contact.setCreatedTime(now);
            contactMapper.insert(contact);
        }

        boolean robotContactExists = isExists(robotId, contact.getId());
        if (!robotContactExists) {
            RobotContact robotContact = new RobotContact()
                    .setId(idGenerator.next())
                    .setRobotId(robotId)
                    .setRobotSerialNo(robotSerialNo)
                    .setContactId(contact.getId())
                    .setContactSerialNo(contact.getSerialNo())
                    .setFriendTime(callback.getTimestamp())
                    .setCreatedTime(now)
                    .setUpdatedTime(now);
            robotContactMapper.insert(robotContact);
        }
    }

    private boolean isExists(String contactId) {
        LambdaQueryWrapper<Contact> wrapper = new LambdaQueryWrapper<Contact>()
                .eq(Contact::getId, contactId);
        return contactMapper.exists(wrapper);
    }

    private boolean isExists(String robotId, String contactId) {
        LambdaQueryWrapper<RobotContact> wrapper = new LambdaQueryWrapper<RobotContact>()
                .eq(RobotContact::getRobotId, robotId)
                .eq(RobotContact::getContactId, contactId);
        return robotContactMapper.exists(wrapper);
    }

    protected Contact buildContact(JSONObject data, LocalDateTime now) {
        return new Contact()
                .setId(data.getString("vcFriendWxId"))
                .setSerialNo(data.getString("vcFriendSerialNo"))
                .setGender(GenderType.valueOf(data.getInteger("nSex")))
                .setAlias(data.getString("vcWxAlias"))
                .setName(data.getString("vcNickName"))
                .setBase64Name(data.getString("vcBase64NickName"))
                .setAvatar(data.getString("vcHeadImgUrl"))
                .setRemarkName(data.getString("vcRemarkName"))
                .setBase64RemarkName(data.getString("vcBase64RemarkName"))
                .setProvince(data.getString("vcProvince"))
                .setCity(data.getString("vcCity"))
                .setCounty(data.getString("vcCounty"))
                .setUpdatedTime(now);
    }
}
